﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;
using AMM.DataAccess.Store;
using AMM.Model;
using AMM.Model.Store;

namespace AMM.Business.Store {
    public class OutsourceQuotationProcessor : BaseProcessor {

        private int _pageSize;
        private OutsourceQuotationRepository _outsourceQuotationRepository;

        public OutsourceQuotationProcessor(int pageSize = int.MaxValue) {
            this._pageSize = pageSize;
            this._outsourceQuotationRepository = new OutsourceQuotationRepository(pageSize);
        }

        public OutsourceQuotation FetchOutsourceQuotationById(int outsourceQuotationId) {
            try {
                return this._outsourceQuotationRepository.FetchOutsourceQuotationById(outsourceQuotationId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定成品报价资料失败！");
            }
        }

        public List<OutsourceQuotation> FetchOutsourceQuotationList() {
            try {
                return this._outsourceQuotationRepository.FetchOutsourceQuotationList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取成品报价列表失败！");
            }
        }

        public OutsourceQuotation FetchOutsourceQuotationByVendorIdAndProductId(int vendorId, int productId) {
            try {
                return this._outsourceQuotationRepository.FetchOutsourceQuotationsByVendorId(vendorId)
                    .Where(x => x.Status == 2)
                    .FirstOrDefault(x => x.ProductId == productId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取成品报价单失败！");
            }
        }

        public List<OutsourceQuotationViewModel> FetchOutsourceQuotationViewModelList(OutsourceQuotationSearchModel searchModel, int pageIndex = 1) {
            try {
                List<OutsourceQuotation> resultList = this._outsourceQuotationRepository.FetchOutsourceQuotationList(searchModel, pageIndex);
                return resultList.Select(x => {
                    return new OutsourceQuotationViewModel() {
                        Key = x.OutsourceQuotationId,
                        VendorCode = x.Vendor.Code,
                        VendorName = x.Vendor.Name,
                        ProductCode = x.Product.Code,
                        Price = x.Price,
                        PriceThresholdValue = x.PriceThresholdValue,
                        PriceGreaterThan = x.PriceGreaterThan,
                        MinimumPackage = x.MinimumPackage,
                        MinimumProcurement = x.MinimumProcurement,
                        PreProcurementCycle = x.PreProcurementCycle,
                        ProcurementCycle = x.ProcurementCycle,
                        IsMultiple = x.IsMultiple,
                        Unit = x.Basic.Code,
                        IsAttach = x.AttachmentId.HasValue,
                        OrderStatus = MethodHelper.GetEnumsDescription(typeof(AuditStatus), x.Status),
                        AuditedBy = x.AuditedBy,
                        AuditedDatetime = x.AuditedDatetime,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime,
                        ModifiedBy = x.ModifiedBy,
                        ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取成品报价列表失败！");
            }
        }

        public void Insert(OutsourceQuotation outsourceQuotation) {
            try {
                outsourceQuotation.OutsourceQuotationId = Guid.NewGuid().GetHashCode();
                outsourceQuotation.CreatedDatetime = DateTime.Now;
                this._outsourceQuotationRepository.Insert(outsourceQuotation);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("插入新成品报价失败！");
            }
        }

        public void Update(OutsourceQuotation outsourceQuotation) {
            try {
                OutsourceQuotation oldOutsourceQuotation = this.FetchOutsourceQuotationById(outsourceQuotation.OutsourceQuotationId);
                if (!oldOutsourceQuotation.RowVersion.SequenceEqual(outsourceQuotation.RowVersion)) {
                    throw this.BuildException("更新成品报价失败，数据不一致性错误！");
                }
                outsourceQuotation.ModifiedDatetime = DateTime.Now;
                this._outsourceQuotationRepository.Update(outsourceQuotation);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("更新成品报价失败！");
            }
        }

        public void Delete(OutsourceQuotation outsourceQuotation) {
            try {
                this._outsourceQuotationRepository.Delete(outsourceQuotation);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除成品报价失败！");
            }
        }

        public void Delete(params int[] outsourceQuotationIds) {
            try {
                this._outsourceQuotationRepository.DeleteMultipleOutsourceQuotation(outsourceQuotationIds);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除成品报价失败！");
            }
        }

        public void Audit(OutsourceQuotation outsourceQuotation) {
            try {
                OutsourceQuotation oldOutsourceQuotation = this.FetchOutsourceQuotationById(outsourceQuotation.OutsourceQuotationId);
                if (!oldOutsourceQuotation.RowVersion.SequenceEqual(outsourceQuotation.RowVersion)) {
                    throw this.BuildException("审核成品报价失败，数据不一致性错误！");
                }
                outsourceQuotation.AuditedDatetime = DateTime.Now;
                this._outsourceQuotationRepository.Update(outsourceQuotation);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("审核物成品价失败！");
            }
        }
    }
}
